package com.alibaba.aone.casestudy.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.aone.casestudy.common.WebConstant;
import com.alibaba.aone.casestudy.common.entity.AoneDepart;
import com.alibaba.aone.casestudy.common.entity.AoneUser;
import com.alibaba.aone.casestudy.common.entity.AoneUserDetail;
import com.alibaba.aone.casestudy.common.entity.DepartSortClass;
import com.alibaba.aone.casestudy.exception.AoneConnectionException;
import com.alibaba.aone.casestudy.service.AoneUserService;
import com.alibaba.aone.casestudy.service.BaseAoneService;

/**
 * �û�Service
 * 
 * @author
 * 
 */
public class AoneUserServiceImpl implements AoneUserService {
	private static final String SQA_DEPARTMENT = "guochenggaijin";
	private static final Log logger = LogFactory
			.getLog(AoneUserServiceImpl.class);

	// @Autowired
	// private AoneUserDao aoneUserDao;

	// @Autowired
	// private AoneDepartService aoneDepartService;

	@Autowired
	private BaseAoneService baseAoneService;

	public AoneUser findAoneUserByAccountId(String realm, String accountId) {

		return null;
	}

	public List<AoneDepart> getAllDepart() {
		String requstParameters = WebConstant.DepartRequest;

		List<AoneDepart> departList = new ArrayList<AoneDepart>();
		try {
			String departResponse = baseAoneService
					.getAoneResponse(requstParameters);
			String[] dateFormats = new String[] { "yyyy-MM-dd HH:mm:ss",
					"yyyy-MM-dd" };
			JSONUtils.getMorpherRegistry().registerMorpher(
					new DateMorpher(dateFormats));
			JSONArray departJsonArray = JSONArray.fromObject(departResponse);
			int size = departJsonArray.size();
			for (int i = 0; i < size; i++) {
				JSONObject jsonObject = departJsonArray.getJSONObject(i);
				AoneDepart aoneDepart = (AoneDepart) JSONObject.toBean(
						jsonObject, AoneDepart.class);
				departList.add(aoneDepart);
			}
			Collections.sort(departList, new DepartSortClass());

		} catch (AoneConnectionException e) {

			logger.error(e);
		}
		return departList;
	}

	public AoneUser getAoneUserByAccount(String accountId) {
		String requstParameters = WebConstant.UserRequest + accountId;
		AoneUser user = null;
		try {
			String userResponse = baseAoneService
					.getAoneResponse(requstParameters);

			JSONObject jsonObject = JSONObject.fromObject(userResponse);

			user = (AoneUser) JSONObject.toBean(jsonObject, AoneUser.class);

		} catch (AoneConnectionException e) {

			logger.error(e);
		}
		return user;
	}

	public String autoCompelteSearchUser(String keyword, int skip, int limit) {

		String requstParameters = WebConstant.QUERY_USER + "&match=" + keyword
				+ "&skip=" + skip + "&max=" + WebConstant.QUERY_MAX;

		List<String> userNameList = new ArrayList<String>();
		try {
			String serverResponse = baseAoneService
					.getAoneResponse(requstParameters);
			JSONArray array = JSONArray.fromObject(serverResponse);
			if (array != null) {
				for (int i = 0; i < array.size(); i++) {
					JSONObject jsonObject = array.getJSONObject(i);
					AoneUserDetail userDetail = (AoneUserDetail) JSONObject
							.toBean(jsonObject, AoneUserDetail.class);

					userNameList.add(userDetail.getUserName());
				}
			}
		} catch (AoneConnectionException e) {
			e.printStackTrace();
		}
		JSONArray arr = JSONArray.fromObject(userNameList);
		String v = arr.toString();
		return v;
	}

	public List<AoneUser> searchUserByKey(String keyword, int skip, int limit) {
		String requstParameters = WebConstant.QUERY_USER + "&match=" + keyword
				+ "&skip=" + skip + "&max=" + WebConstant.QUERY_MAX;
		List<AoneUser> userList = new ArrayList<AoneUser>();
		try {
			String serverResponse = baseAoneService
					.getAoneResponse(requstParameters);
			JSONArray array = JSONArray.fromObject(serverResponse);
			if (array != null) {
				for (int i = 0; i < array.size(); i++) {
					JSONObject jsonObject = array.getJSONObject(i);
					AoneUserDetail userDetail = (AoneUserDetail) JSONObject
							.toBean(jsonObject, AoneUserDetail.class);

					AoneUser user = new AoneUser();
					user.setAccountId(userDetail.getAccountId());
					user.setAliTalking(userDetail.getAliTalking());
					user.setDepartment(userDetail.getDepartment());
					user.setEmail(userDetail.getEmail());
					user.setMobile(userDetail.getMobile());
					user.setPassword(userDetail.getPassword());
					user.setTbTalking(userDetail.getTbTalking());
					user.setUserName(userDetail.getUserName());
					userList.add(user);
				}
			}
		} catch (AoneConnectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return userList;
	}

	public String queryProductLine(String siteId) {
		String requstParameters = WebConstant.QUERY_PRODUCTLINE + "&siteId="
				+ siteId;

		String serverResponse = "";
		try {
			serverResponse = baseAoneService.getAoneResponse(requstParameters);
			serverResponse = serverResponse.trim();
		} catch (AoneConnectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return serverResponse;
	}
}
